package com.ruoyi.web.controller.check.patient;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.YkTokenService;
import com.ruoyi.system.domain.Patient;
import com.ruoyi.system.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/check/patient")
public class PatientController extends BaseController {

    @Autowired
    private PatientService patientService;

    /**
     * 查询患者主数据列表
     */
    @PreAuthorize("@ss.hasPermi('check:patient:list')")
    @GetMapping("/list")
    public TableDataInfo list(Patient patient)
    {
        startPage();
        List<Patient> list = new ArrayList<>();
        LoginUser loginUser = SecurityUtils.getLoginUser();//获取当前登录信息
        SysUser sysUser = loginUser.getUser();
        if(sysUser != null){
            String isRegister = sysUser.getIsRegister() == null? "0":sysUser.getIsRegister() ;//是否注册 1已注册，0未注册
            if(StringUtils.isNotEmpty(isRegister) && isRegister.equals("1")){//注册，则代表客户
                patient.setPatientName(patient.getSearchValue());
                list = patientService.selectPatientListBySjdwNameAndSjys(patient,sysUser);
            }else{
                list = patientService.selectPatientList(patient);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出患者主数据列表
     */
    @PreAuthorize("@ss.hasPermi('check:patient:export')")
    @Log(title = "患者主数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Patient patient)
    {
        List<Patient> list = patientService.selectPatientList(patient);
        ExcelUtil<Patient> util = new ExcelUtil<Patient>(Patient.class);
        util.exportExcel(response, list, "患者主数据数据");
    }

    /**
     * 获取患者主数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('check:patient:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(patientService.selectPatientById(id));
    }

    /**
     * 新增患者主数据
     */
    @PreAuthorize("@ss.hasPermi('check:patient:add')")
    @Log(title = "患者主数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Patient patient)
    {
        return toAjax(patientService.insertPatient(patient));
    }

    /**
     * 修改患者主数据
     */
    @PreAuthorize("@ss.hasPermi('check:patient:edit')")
    @Log(title = "患者主数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Patient patient)
    {
        return toAjax(patientService.updatePatient(patient));
    }

    /**
     * 删除患者主数据
     */
    @PreAuthorize("@ss.hasPermi('check:patient:remove')")
    @Log(title = "患者主数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(patientService.deletePatientByIds(ids));
    }
}
